package database.demo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ImportantOperations {

	// Database fields
	private DataBaseWrapperImportant dbHelper;
	private String[] IMPORTANT_TABLE_COLUMNS = { DataBaseWrapperImportant.IMPORTANT_ID, 
			DataBaseWrapperImportant.IMPORTANT_NAME,DataBaseWrapperImportant.IMPORTANT_ROLE,
			DataBaseWrapperImportant.IMPORTANT_PHONE, DataBaseWrapperImportant.IMPORTANT_EMAIL,
			DataBaseWrapperImportant.IMPORTANT_ADDRESS};
	private String[] IMPORTANT_TABLE_ID = { DataBaseWrapperImportant.IMPORTANT_ID};
	private SQLiteDatabase database;

	public ImportantOperations(Context context) {
		dbHelper = new DataBaseWrapperImportant(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public ImportantContant addImportantContant(String ID, String name, String role, String phone, String email, String address) {
		ImportantContant newComment = null;
		ContentValues values = new ContentValues();
		values.put(DataBaseWrapperImportant.IMPORTANT_ID, ID);
		values.put(DataBaseWrapperImportant.IMPORTANT_NAME, name);
		values.put(DataBaseWrapperImportant.IMPORTANT_ROLE, role);
		values.put(DataBaseWrapperImportant.IMPORTANT_PHONE, phone);
		values.put(DataBaseWrapperImportant.IMPORTANT_EMAIL, email);
		values.put(DataBaseWrapperImportant.IMPORTANT_ADDRESS, address);
		
		
		String where = "_id like ?";
		String[] whereArgs = new String[] {ID};
		if(database.query(true, DataBaseWrapperImportant.IMPORTANT, IMPORTANT_TABLE_COLUMNS, 
				where, whereArgs, null, null, null, null).getCount() < 1 ){
		
		database.insert(DataBaseWrapperImportant.IMPORTANT, null, values);

		// now that the passenger is created return it ...
		Cursor cursor = database.query(DataBaseWrapperImportant.IMPORTANT,
				IMPORTANT_TABLE_COLUMNS, DataBaseWrapperImportant.IMPORTANT_ID + " = "
						+ ID, null, null, null, null);
		cursor.moveToFirst();
		newComment = parseImportant(cursor);
		cursor.close();
		}
		return newComment;
	}
	
	public void updateImportantContact(String tmpid, String tmpName, String tmpRole, String tmpPhone, String tmpMail, String tmpAddress) {
		ContentValues values = new ContentValues();
		values.put(DataBaseWrapperImportant.IMPORTANT_ID, tmpid);
		values.put(DataBaseWrapperImportant.IMPORTANT_NAME, tmpName);
		values.put(DataBaseWrapperImportant.IMPORTANT_ROLE, tmpRole);
		values.put(DataBaseWrapperImportant.IMPORTANT_PHONE, tmpPhone);
		values.put(DataBaseWrapperImportant.IMPORTANT_EMAIL, tmpMail);
		values.put(DataBaseWrapperImportant.IMPORTANT_ADDRESS, tmpAddress);
		String where = "_id like ?";
		String[] whereArgs = new String[] {String.valueOf(tmpid)};
		database.update(DataBaseWrapperImportant.IMPORTANT, values,where, whereArgs);
	}

	public boolean deleteImportant(String importantID) {
		if (database.delete(DataBaseWrapperImportant.IMPORTANT, DataBaseWrapperImportant.IMPORTANT_ID
				+ " = " + importantID, null)>0){
			System.out.println("Contact deleted with id: " + importantID);
			return true;
		}
		else return false;
	}
	

	public Cursor getMaxID() {
		Cursor c = 	database.query(DataBaseWrapperImportant.IMPORTANT, null, "_id=(SELECT MAX(_id))", null, null, null, null);
	if (c != null) {
		c.moveToLast();
	}
	return c;
}
	
	
	public Cursor getAllImportant() {
		Cursor cursor = database.query(DataBaseWrapperImportant.IMPORTANT,
				IMPORTANT_TABLE_COLUMNS, null, null, null, null, DataBaseWrapperImportant.IMPORTANT_NAME);
		if (cursor!=null){
			cursor.moveToFirst();
		}
		return cursor;
	}

	// Get a specific row (by tmpId)
		public Cursor getImportantContact(String tmpId) {
			String where = DataBaseWrapperImportant.IMPORTANT_ID + " like " + tmpId;
			Cursor c = 	database.query(true, DataBaseWrapperImportant.IMPORTANT, IMPORTANT_TABLE_COLUMNS, 
							where, null, null, null, null, null);
			if (c != null) {
				c.moveToFirst();
			}
			return c;
		}
		
				
				
		private ImportantContant parseImportant(Cursor cursor) {
		ImportantContant important = new ImportantContant();
		important.setID((cursor.getString(0)));
		important.setName(cursor.getString(1));
		important.setRole(cursor.getString(2));
		important.setPhone(cursor.getString(3));
		important.setEmail(cursor.getString(4));
		important.setAddress(cursor.getString(5));
		
		return important;
	}
	
	
}